/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KlasKiesPagina.java
 *
 * Created on 18-mei-2011, 11:45:19
 */
package view;

import connections.Query;
import java.awt.Color;
import java.util.List;
import javax.swing.JOptionPane;
import main.ApplicatieMain;

/**
 *
 * @author Nick
 */
public class KlasKiesPagina extends javax.swing.JPanel {

    private Query query;
    private boolean initializing;
    private ApplicatieMain applicatieMain;

    /** Creates new form KlasKiesPagina */
    public KlasKiesPagina() {
        applicatieMain = ApplicatieMain.getInstance();
        query = applicatieMain.getQuery();
        initializing = true;

        initComponents();
        initKlassenLijst();
        cmbKlas.setSelectedIndex(-1);
        initializing = false;


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        btnSpeel = new javax.swing.JButton();
        lblKlas = new javax.swing.JLabel();
        cmbKlas = new javax.swing.JComboBox();
        lblNaam = new javax.swing.JLabel();
        cmbLeerling = new javax.swing.JComboBox();
        btnKlassenBeheer = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btnProfiel = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(1219, 502));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 721, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        jPanel7.setBackground(new java.awt.Color(255, 255, 255));
        jPanel7.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(183, 234, 0), 10, true));

        btnSpeel.setBackground(new java.awt.Color(153, 255, 51));
        btnSpeel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        btnSpeel.setText("Speel!");
        btnSpeel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSpeel.setEnabled(false);
        btnSpeel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnSpeelEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnSpeelExit(evt);
            }
        });
        btnSpeel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSpeelActionPerformed(evt);
            }
        });

        lblKlas.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblKlas.setText("Kies je klas:");

        cmbKlas.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbKlasItemStateChanged(evt);
            }
        });
        cmbKlas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbKlasActionPerformed(evt);
            }
        });

        lblNaam.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblNaam.setText("Naam:");
        lblNaam.setEnabled(false);

        cmbLeerling.setEnabled(false);
        cmbLeerling.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbLeerlingItemStateChanged(evt);
            }
        });
        cmbLeerling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbLeerlingActionPerformed(evt);
            }
        });

        btnKlassenBeheer.setFont(new java.awt.Font("Tahoma", 0, 10));
        btnKlassenBeheer.setText("Klassenbeheer");
        btnKlassenBeheer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKlassenBeheerActionPerformed(evt);
            }
        });

        jButton1.setText("Logout");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnProfiel.setBackground(new java.awt.Color(153, 255, 51));
        btnProfiel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        btnProfiel.setText("Profiel");
        btnProfiel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnProfiel.setEnabled(false);
        btnProfiel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnProfielEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnProfielExit(evt);
            }
        });
        btnProfiel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProfielActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnSpeel, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnProfiel, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnKlassenBeheer))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGap(239, 239, 239)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblNaam)
                            .addComponent(lblKlas))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbKlas, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbLeerling, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                        .addContainerGap(598, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addGap(57, 57, 57)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbKlas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblKlas, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNaam)
                    .addComponent(cmbLeerling, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(165, 165, 165)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnKlassenBeheer)
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnProfiel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSpeel, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 721, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 69, Short.MAX_VALUE)
        );

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 208, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 491, Short.MAX_VALUE)
        );

        jPanel6.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 258, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 491, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSpeelEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSpeelEntered
        btnSpeel.setBackground(new Color(204, 235, 204));
}//GEN-LAST:event_btnSpeelEntered

    private void btnSpeelExit(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSpeelExit
        btnSpeel.setBackground(new Color(153, 255, 51));
}//GEN-LAST:event_btnSpeelExit

    private void btnSpeelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSpeelActionPerformed
        if (query.getStatus((String) cmbLeerling.getSelectedItem()) != 1) {
            applicatieMain.setUsername((String) cmbLeerling.getSelectedItem());
            query.setStatus(1); //set the user as logged in/online
            applicatieMain.showPage(new SpelerOptiesPagina());
        } else {
            JOptionPane.showMessageDialog(this,"Deze leerling is al ingelogd","FOUT", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_btnSpeelActionPerformed

    private void cmbKlasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbKlasActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbKlasActionPerformed

    private void cmbKlasItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbKlasItemStateChanged

        if (!initializing) {
            if (!cmbLeerling.isEnabled()) {
                cmbLeerling.setEnabled(true);
                lblNaam.setEnabled(true);
            }
            String selectedKlas = (String) cmbKlas.getSelectedItem();
            applicatieMain.setSelectedKlasID(query.getKlasID(selectedKlas));
            initLeerlingLijst(selectedKlas);
        }
    }//GEN-LAST:event_cmbKlasItemStateChanged

    private void btnKlassenBeheerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKlassenBeheerActionPerformed
        applicatieMain.showPage(new KlassenBeheerLogin());        // TODO add your handling code here:
    }//GEN-LAST:event_btnKlassenBeheerActionPerformed

    private void cmbLeerlingItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbLeerlingItemStateChanged
        if (cmbLeerling.getSelectedIndex() != -1) {
            btnSpeel.setEnabled(true);
            btnProfiel.setEnabled(true);
        } else {
            btnSpeel.setEnabled(false);
            btnProfiel.setEnabled(false);
        }
    }//GEN-LAST:event_cmbLeerlingItemStateChanged

    private void cmbLeerlingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbLeerlingActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbLeerlingActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        query.setStatus(0);
        applicatieMain.setBrinNummer(0);
        applicatieMain.showPage(new HoofdPagina());

    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnProfielEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnProfielEntered
        btnProfiel.setBackground(new Color(204, 235, 204));
    }//GEN-LAST:event_btnProfielEntered

    private void btnProfielExit(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnProfielExit
        btnProfiel.setBackground(new Color(153, 255, 51));
    }//GEN-LAST:event_btnProfielExit

    private void btnProfielActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProfielActionPerformed
        applicatieMain.setUsername((String) cmbLeerling.getSelectedItem());
        applicatieMain.showPage(new ProfilePage());
    }//GEN-LAST:event_btnProfielActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnKlassenBeheer;
    private javax.swing.JButton btnProfiel;
    private javax.swing.JButton btnSpeel;
    private javax.swing.JComboBox cmbKlas;
    private javax.swing.JComboBox cmbLeerling;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JLabel lblKlas;
    private javax.swing.JLabel lblNaam;
    // End of variables declaration//GEN-END:variables

    private void initKlassenLijst() {
        List<String> klassenLijst = query.getKlassenLijst(applicatieMain.getBrinNummer());
        for (String klas : klassenLijst) {
            cmbKlas.addItem(klas);
        }
    }

    private void initLeerlingLijst(String selectedKlas) {

        List<String> leerlingLijst = query.getLeerlingLijst(applicatieMain.getBrinNummer(), selectedKlas);
        cmbLeerling.removeAllItems();
        for (String leerling : leerlingLijst) {
            cmbLeerling.addItem(leerling);
        }
    }
}
